package com.luyu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luyu.entity.Permission;
import com.luyu.entity.RolePermission;
import com.luyu.mapper.PermissionMapper;
import com.luyu.service.PermissionService;
import com.luyu.service.RolePermissionService;
import jakarta.annotation.Resource;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HASEE
 * @description 针对表【permission】的数据库操作Service实现
 * @createDate 2024-03-11 21:37:12
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    RolePermissionService rolePermissionService;

    @Override
    public List<SimpleGrantedAuthority> listByRoleIds(List<Long> roleIdList) {
        List<Long> permissionIds = rolePermissionService
                .lambdaQuery()
                .in(RolePermission::getRoleId, roleIdList)
                .list()
                .stream()
                .map(RolePermission::getPermissionId)
                .distinct()
                .toList();
        return this.listByIds(permissionIds)
                .stream()
                .map(entity -> new SimpleGrantedAuthority(entity.getCode()))
                .toList();
    }

    @Override
    public List<Long> listRoleIdsByPermissionCode(String permissionCode) {
        Permission permission = this.lambdaQuery().eq(Permission::getCode, permissionCode)
                .one();
        if (permission == null) {
            return new ArrayList<>();
        }
        return rolePermissionService.listRoleIdsByPermissionIds(permission.getId());
    }


}




